#include <stdlib.h>
#include "stdarg.h"
#include "string.h"
#include "vpi_user.h"
#include "veriuser.h"
#include "params.h"

int get_options()
{

	// setup some default parameters
	default_params();

	// check for testbench command line parameters
	/*char *s;
	int seed = 0;

	s = (char *)mc_scan_plusargs("tb_seed+");
	if (s != NULL)
		seed = atoi(s);
	vpi_printf("Setting seed = %d\n", seed);
	srandom(seed);

	s = (char *)mc_scan_plusargs("tb_verbose+");
	if (s != NULL) {
		vpi_printf("Setting verbose = %s\n", s);
		params.verbose = atoi(s);
	}

	s = (char *)mc_scan_plusargs("tb_cfgfile+");
	if (s != NULL) {
		// open and parse the cfgfile
		vpi_printf("Setting cfgfile = %s\n", s);
		if (!read_params(s)) {
			vpi_printf
			    ("Error reading configuration file. Terminating.\n");
			return 0;
		}
	}

	if (params.auto_configure)
		auto_configure();
	print_params();

	if (params.verbose)
		vpi_printf("%9s : INFORMATION\n", "CYCLE");
*/
	return 1;
}

void default_params()
{
	params.verbose = 1;
	params.auto_configure = 0;
	params.max_cycles = 10;

	params.density_inst_i_valid = 0.9;
	params.density_data_i_valid = 0.9;
	params.density_reset = 0.001;

	params.inst_mask_inst_i_line = 0xfc631ffffc631fff;

}

int read_params(char *s)
{
	if (s == NULL) {
		vpi_printf("s=NULL");
		return 0;
	} else {
		//open config file
		//read line by line
		//set all parameters to appropriate values

		FILE *file = fopen(s, "r");
		if (file != NULL) {
			char line[128];
			char *pch;
			while (fgets(line, sizeof line, file) != NULL) {
				pch = strtok(line, " ");
				if (pch != NULL) {
					//get value of next var
					if (strcmp(pch, "max_cycles") == 0) {
						pch = strtok(NULL, " ");
						if (pch != NULL)
							params.max_cycles =
							    atol(pch);
					} else if (strcmp(pch, "auto_configure")
						   == 0) {
						pch = strtok(NULL, " ");
						if (pch != NULL)
							params.auto_configure =
							    atoi(pch);
					} else
					    if (strcmp
						(pch,
						 "density_inst_i_valid") == 0) {
						pch = strtok(NULL, " ");
						if (pch != NULL)
							params.density_inst_i_valid
							    = atof(pch);
					} else
					    if (strcmp
						(pch,
						 "density_data_i_valid") == 0) {
						pch = strtok(NULL, " ");
						if (pch != NULL)
							params.density_data_i_valid
							    = atof(pch);
					} else if (strcmp(pch, "density_reset")
						   == 0) {
						pch = strtok(NULL, " ");
						if (pch != NULL)
							params.density_reset =
							    atof(pch);
					}

					else if (strcmp
						 (pch,
						  "inst_mask_inst_i_line") ==
						 0) {
						pch = strtok(NULL, " ");
						if (pch != NULL)
							params.inst_mask_inst_i_line
							    =
							    strtoll(pch, NULL,
								    16);
					}

				}

				if (params.auto_configure) {
					auto_configure();
				}
			}
			fclose(file);
		} else {
			return 0;
		}
	}
	return 1;
}

void auto_configure()
{
	params.max_cycles = 20000;
	params.density_inst_i_valid = (rand() % 100) / 100;
	params.density_data_i_valid = (rand() % 100) / 100;
	params.density_reset = (rand() % 10) / 100;	// constrain
	params.inst_mask_inst_i_line = 0xfc631ffffc631fff;

}

void print_params()
{
	vpi_printf("Setting %s = %lld\n", "max_cycles", params.max_cycles);
	vpi_printf("Setting %s = %d\n", "auto_configure",
		   params.auto_configure);

	vpi_printf("Setting %s = %3.2f\n", "density_inst_i_valid",
		   params.density_inst_i_valid);
	vpi_printf("Setting %s = %3.2f\n", "density_data_i_valid",
		   params.density_data_i_valid);

	vpi_printf("Setting %s = %3.3f\n", "density_reset",
		   params.density_reset);

	vpi_printf("Setting %s = 0x%lld\n", "inst_mask_inst_i_line",
		   params.inst_mask_inst_i_line);

}
